package com.himaci.gtbattle.resource;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 * Kelas singleton untuk menyimpan seluruh asset
 *
 * @author XNPSDN
 */
public class Assets {

    private static Assets instance;
    private AssetManager manager;
    // daftar assets
    public static final String FILEPATH_ABOUT = "./resources/image/about.png";
    public static final String FILEPATH_GAME_BG = "./resources/image/game_bg.png";
    public static final String FILEPATH_LOADING = "./resources/image/loading.png";
    public static final String FILEPATH_MAIN_MENU = "./resources/image/main_menu.png";
    public static final String FILEPATH_SPRITES = "./resources/image/sprites.png";
    public static final String FILEPATH_DIALOG_BOX = "./resources/image/dialog_box.png";    
    public static final String FILEPATH_FONT_NORMAL = "./resources/font/normal_text.fnt";
    public static final String FILEPATH_FONT_IMAGE = "./resources/font/image_text.fnt";
    
    public Texture aboutTexture;
    public TextureRegion about;
    public Texture gameBgTexture;
    public TextureRegion gameBg;
    public Texture loadingTexture;
    public TextureRegion loading;
    public Texture mainMenuTexture;
    public TextureRegion mainMenu;
    public Texture spritesTexture;
    public TextureRegion archer;
    public TextureRegion mage;
    public TextureRegion priest;
    public Animation archerReady;
    public Animation archerDead;
    public Animation archerAttack;
    public Animation archerSpecial;    
    public Animation mageReady;
    public Animation mageDead;
    public Animation mageAttack;
    public Animation mageSpecial;
    public Animation priestReady;
    public Animation priestDead;
    public Animation priestAttack;
    public Animation priestSpecial;
    public TextureRegion skillButton[][];
    public TextureRegion okButton;   
    public TextureRegion cancelButton;
    public TextureRegion disableIcon;
    public TextureRegion charPointer;
    public TextureRegion targetPointer;
    public TextureRegion skipButton;
    public TextureRegion projectile;
    public Texture dialogBoxTexture;
    public TextureRegion dockBox;
    public TextureRegion dialogBox;
    public TextureRegion pauseBox;
    public TextureRegion resumeButton;
    public TextureRegion homeButton;
    public TextureRegion pauseButton;
    public BitmapFont fontNormal;
    public BitmapFont fontImage;

    /*
     * Private konstruktor
     */
    private Assets() {
	manager = new AssetManager();
	skillButton = new TextureRegion[3][3];
    }

    /**
     * Mendapatkan singleton instance
     * @return Instance dari kelas Assets
     */
    public static synchronized Assets getInstance() {
	if (instance == null) {
	    instance = new Assets();
	}
	return instance;
    }

    /**
     * Meload asset yang diperlukan
     */
    public void load() {
	manager.load(FILEPATH_ABOUT, Texture.class);
	manager.load(FILEPATH_GAME_BG, Texture.class);
	manager.load(FILEPATH_LOADING, Texture.class);
	manager.load(FILEPATH_MAIN_MENU, Texture.class);
	manager.load(FILEPATH_SPRITES, Texture.class);
	manager.load(FILEPATH_DIALOG_BOX, Texture.class);
	
	manager.load(FILEPATH_FONT_NORMAL, BitmapFont.class);
	manager.load(FILEPATH_FONT_IMAGE, BitmapFont.class);
	
	manager.finishLoading();
	
	aboutTexture = manager.get(FILEPATH_ABOUT, Texture.class);
	gameBgTexture = manager.get(FILEPATH_GAME_BG, Texture.class);
	loadingTexture = manager.get(FILEPATH_LOADING, Texture.class);
	mainMenuTexture = manager.get(FILEPATH_MAIN_MENU, Texture.class);
	spritesTexture = manager.get(FILEPATH_SPRITES, Texture.class);
	dialogBoxTexture = manager.get(FILEPATH_DIALOG_BOX, Texture.class);
	fontNormal = manager.get(FILEPATH_FONT_NORMAL, BitmapFont.class);
	fontImage = manager.get(FILEPATH_FONT_IMAGE, BitmapFont.class);	
	
	about = new TextureRegion(aboutTexture, 0, 0, 480, 320);
	gameBg = new TextureRegion(gameBgTexture, 0, 0, 480, 320);
	loading = new TextureRegion(loadingTexture, 0, 0, 480, 320);
	mainMenu = new TextureRegion(mainMenuTexture, 0, 0, 480, 320);
	
	archer = new TextureRegion(spritesTexture, 0, 390, 50, 50);
	mage = new TextureRegion(spritesTexture, 50, 390, 50, 50);
	priest = new TextureRegion(spritesTexture, 100, 390, 50, 50);
	
	archerReady = new Animation(0.2f,
                        new TextureRegion(spritesTexture, 0, 0, 40, 80),
                        new TextureRegion(spritesTexture, 40, 0, 40, 80),
                        new TextureRegion(spritesTexture, 80, 0, 40, 80));
	archerDead = new Animation(0.2f,
                        new TextureRegion(spritesTexture, 120, 0, 40, 80),
                        new TextureRegion(spritesTexture, 160, 0, 40, 80),
                        new TextureRegion(spritesTexture, 200, 0, 40, 80));
	archerAttack = new Animation(0.5f,
                        new TextureRegion(spritesTexture, 240, 0, 40, 80),
                        new TextureRegion(spritesTexture, 280, 0, 40, 80),
                        new TextureRegion(spritesTexture, 320, 0, 40, 80));
	archerSpecial = archerAttack;
//	archerSpecial = new Animation(0.2f,
//                        new TextureRegion(spritesTexture, 360, 0, 40, 80),
//                        new TextureRegion(spritesTexture, 400, 0, 40, 80),
//                        new TextureRegion(spritesTexture, 440, 0, 40, 80));
	
	mageReady = new Animation(0.2f,
                        new TextureRegion(spritesTexture, 0, 80, 40, 80),
                        new TextureRegion(spritesTexture, 40, 80, 40, 80),
                        new TextureRegion(spritesTexture, 80, 80, 40, 80));
	mageDead = new Animation(0.2f,
                        new TextureRegion(spritesTexture, 120, 80, 40, 80),
                        new TextureRegion(spritesTexture, 160, 80, 40, 80),
                        new TextureRegion(spritesTexture, 200, 80, 40, 80));
	mageAttack = new Animation(0.5f,
                        new TextureRegion(spritesTexture, 240, 80, 40, 80),
                        new TextureRegion(spritesTexture, 280, 80, 40, 80),
                        new TextureRegion(spritesTexture, 320, 80, 40, 80));
	mageSpecial = mageAttack;
//	mageSpecial = new Animation(0.2f,
//                        new TextureRegion(spritesTexture, 360, 80, 40, 80),
//                        new TextureRegion(spritesTexture, 400, 80, 40, 80),
//                        new TextureRegion(spritesTexture, 440, 80, 40, 80));
	
	priestReady = new Animation(0.2f,
                        new TextureRegion(spritesTexture, 0, 160, 40, 80),
                        new TextureRegion(spritesTexture, 40, 160, 40, 80),
                        new TextureRegion(spritesTexture, 80, 160, 40, 80));
	priestDead = new Animation(0.2f,
                        new TextureRegion(spritesTexture, 120, 160, 40, 80),
                        new TextureRegion(spritesTexture, 160, 160, 40, 80),
                        new TextureRegion(spritesTexture, 200, 160, 40, 80));
	priestAttack = new Animation(0.5f,
                        new TextureRegion(spritesTexture, 240, 160, 40, 80),
                        new TextureRegion(spritesTexture, 280, 160, 40, 80),
                        new TextureRegion(spritesTexture, 320, 160, 40, 80));
	priestSpecial = priestAttack;
//	priestSpecial = new Animation(0.2f,
//                        new TextureRegion(spritesTexture, 360, 160, 40, 80),
//                        new TextureRegion(spritesTexture, 400, 160, 40, 80),
//                        new TextureRegion(spritesTexture, 440, 160, 40, 80));
	
	skillButton[0][0] = new TextureRegion(spritesTexture, 0, 240, 50, 50);
	skillButton[0][1] = new TextureRegion(spritesTexture, 50, 240, 50, 50);
	skillButton[0][2] = new TextureRegion(spritesTexture, 100, 240, 50, 50);
	skillButton[1][0] = new TextureRegion(spritesTexture, 0, 290, 50, 50);
	skillButton[1][1] = new TextureRegion(spritesTexture, 50, 290, 50, 50);
	skillButton[1][2] = new TextureRegion(spritesTexture, 100, 290, 50, 50);
	skillButton[2][0] = new TextureRegion(spritesTexture, 0, 340, 50, 50);
	skillButton[2][1] = new TextureRegion(spritesTexture, 50, 340, 50, 50);
	skillButton[2][2] = new TextureRegion(spritesTexture, 100, 340, 50, 50);
	
	okButton = new TextureRegion(spritesTexture, 300, 240, 50, 50); 
	cancelButton = new TextureRegion(spritesTexture, 350, 240, 50, 50); 
	disableIcon = new TextureRegion(spritesTexture, 400, 240, 50, 50); 
	charPointer = new TextureRegion(spritesTexture, 300, 290, 40, 40); 
	targetPointer = new TextureRegion(spritesTexture, 340, 290, 40, 40); 
	skipButton = new TextureRegion(spritesTexture, 300, 330, 80, 40); 
	projectile = new TextureRegion(spritesTexture, 380, 290, 10, 10);
	
	dockBox = new TextureRegion(dialogBoxTexture, 0, 0, 480, 60);
	dialogBox = new TextureRegion(dialogBoxTexture, 0, 60, 300, 200);
	pauseBox = new TextureRegion(dialogBoxTexture, 0, 260, 200, 100);
	resumeButton = new TextureRegion(dialogBoxTexture, 0, 360, 80, 40);
	homeButton = new TextureRegion(dialogBoxTexture, 80, 360, 80, 40);
	pauseButton = new TextureRegion(dialogBoxTexture, 0, 400, 50, 50);
    }

    /**
     * Unload asset yang diperlukan untuk mode tertentu
     */
    public void unload() {
	manager.unload(FILEPATH_ABOUT);
	manager.unload(FILEPATH_GAME_BG);
	manager.unload(FILEPATH_LOADING);
	manager.unload(FILEPATH_MAIN_MENU);
	manager.unload(FILEPATH_SPRITES);
	manager.unload(FILEPATH_DIALOG_BOX);
	manager.unload(FILEPATH_FONT_NORMAL);
	manager.unload(FILEPATH_FONT_IMAGE);
    }

    /**
     * Dispose seluruh assets
     */
    public void dispose() {
	manager.dispose();
    }
}
